package com.rose.mall.product.service.impl;

import com.rose.mall.product.entity.AttrEntity;
import com.rose.mall.product.service.AttrAttrgroupRelationService;
import com.rose.mall.product.service.AttrService;
import com.rose.mall.product.vo.AttrGroupWithAttrsVo;
import com.rose.mall.product.vo.SkuItemVo;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rose.common.utils.PageUtils;
import com.rose.common.utils.Query;

import com.rose.mall.product.dao.AttrGroupDao;
import com.rose.mall.product.entity.AttrGroupEntity;
import com.rose.mall.product.service.AttrGroupService;


@Data
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // 提供搜索的功能和模糊查询
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->
                    obj.eq("attr_group_id", key)
                            .or().like("attr_group_name", key)
                            .or().like("descript",key)
            );
        }
        //分为查全部和根据categoryId查
        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据三级分类id来查出所有的分组以及组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 1.查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 2.查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map((item) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,attrGroupWithAttrsVo);

            // 查询出属性分组的id然后赋值
            List<AttrEntity> relationAttr = attrService.getRelationAttr(item.getAttrGroupId());
            
            attrGroupWithAttrsVo.setAttrs(relationAttr);

            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SkuItemVo.SpuItemBaseAttrVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 1.查出当前spu对应的所有属性的分组信息以及当前分组下所有属性对应的值
        // pms_product_attr_value商品的属性表在这个里面
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SkuItemVo.SpuItemBaseAttrVo>  vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;
    }

//    @Override
//    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
//        //如果没有三级分类，那么三级分类id 就传0  0就是查所有
//
//        if ( catelogId == 0){
//            IPage<AttrGroupEntity> page = this.page(
//                    new Query<AttrGroupEntity>().getPage(params),
//                    new QueryWrapper<AttrGroupEntity>()
//            );
//            return new PageUtils(page) ;
//        }else{
//            String key =(String)params.get("key");
//            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId);
//            if (!StringUtils.isEmpty(key)){
//                queryWrapper.and((obj)->{
//                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
//
//                });
//
//            }
//            IPage<AttrGroupEntity> page = this.page(
//                    new Query<AttrGroupEntity>().getPage(params),
//                    queryWrapper
//            );
//            return new PageUtils(page) ;
//
//        }
//
//
//    }

}